package com.plate.modules.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plate.common.core.result.Result;
import com.plate.common.web.annotation.AutoLog;
import com.plate.common.web.domain.LoginVal;
import com.plate.common.web.utils.OauthUtils;
import com.plate.modules.system.domain.bo.SysUserQueryBO;
import com.plate.modules.system.domain.bo.SysUserRoleBO;
import com.plate.modules.system.domain.bo.SysUserSaveBO;
import com.plate.modules.system.domain.po.SysUser;
import com.plate.modules.system.domain.po.SysUserRole;
import com.plate.modules.system.domain.vo.SysUserVO;
import com.plate.modules.system.service.ISysUserRoleService;
import com.plate.modules.system.service.ISysUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SysUserController
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年10月18日 21:37
 * @Version 1.0
 */

@RestController
@RequestMapping("sys/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @PostMapping("/getUserByUsername/{username}")
    public Result<SysUser> getUserByUsername(@PathVariable("username") String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(wrapper);
        return Result.SUCCESS(sysUser);
    }


    @AutoLog(dictCode = "hhshshhs")
    @PostMapping("/list")
    public Result list(@RequestBody SysUserQueryBO queryBO) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isBlankIfStr(queryBO.getUsername())) {
            wrapper.like(SysUser::getUsername, queryBO.getUsername());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getNickName())) {
            wrapper.like(SysUser::getNickName, queryBO.getNickName());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getPhone())) {
            wrapper.like(SysUser::getPhone, queryBO.getPhone());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getUserType())) {
            wrapper.eq(SysUser::getUserType, queryBO.getUserType());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getStatus())) {
            wrapper.eq(SysUser::getStatus, queryBO.getStatus());
        }
        Page<SysUser> page = new Page<>(queryBO.getPage(), queryBO.getSize());
        Page<SysUser> userPage = sysUserService.page(page, wrapper);
        return Result.SUCCESS(userPage);
    }

    @PostMapping("/update")
    public Result udateUser(@RequestBody SysUserSaveBO saveBO) {
        if (StrUtil.isBlankIfStr(saveBO.getId())) {
            // 新增
            SysUser sysUser = new SysUser();
            sysUser.setUsername(saveBO.getUsername());
            sysUser.setNickName(saveBO.getNickName());
            sysUser.setPassword(saveBO.getPassword());
            sysUser.setUserType(saveBO.getUserType());
            sysUser.setAvatar(saveBO.getAvatar());
            sysUser.setBirthday(saveBO.getBirthday());
            sysUser.setEmail(saveBO.getEmail());
            sysUser.setSex(saveBO.getSex());
            sysUser.setPhone(saveBO.getPhone());
            sysUser.setStatus(saveBO.getStatus());
            sysUserService.isSave(sysUser);
        } else {
            // 修改
            SysUser sysUser = sysUserService.getById(saveBO.getId());
            sysUser.setNickName(saveBO.getNickName());
            sysUser.setUserType(saveBO.getUserType());
            sysUser.setAvatar(saveBO.getAvatar());
            sysUser.setBirthday(saveBO.getBirthday());
            sysUser.setEmail(saveBO.getEmail());
            sysUser.setSex(saveBO.getSex());
            sysUser.setPhone(saveBO.getPhone());
            sysUser.setStatus(saveBO.getStatus());
            sysUserService.isUpdate(sysUser);
        }
        return Result.SUCCESS();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser == null) {
            return Result.FAIL("");
        }
        boolean remove = sysUserService.removeById(id);
        return Result.SUCCESS(remove);
    }

    @PostMapping("/getUserInfo")
    public Result getUserInfo() {
        LoginVal currentUser = OauthUtils.getCurrentUser();
        SysUserVO vo = sysUserService.getUserInfoById(currentUser.getUserId());
        return Result.SUCCESS(vo);
    }


    @PostMapping("/getBindUserLit")
    public Result getBindUserLit(@RequestBody SysUserQueryBO queryBO) {
        Page<SysUser> userPage =  sysUserService.getBindUserLit(queryBO);
        return Result.SUCCESS(userPage);
    }

    @PostMapping("unBindUserRoleList")
    public Result unBindUserRoleList(@RequestBody SysUserQueryBO queryBO) {
        Page<SysUser> userPage =  sysUserService.getUnBindUserLit(queryBO);
        return Result.SUCCESS(userPage);
    }

    @PostMapping("/delBindUser")
    public Result delBindUser(@RequestBody SysUserRoleBO sysUserRoleBO) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, sysUserRoleBO.getUserId());
        wrapper.eq(SysUserRole::getRoleId, sysUserRoleBO.getRoleId());
        boolean remove = sysUserRoleService.delete(sysUserRoleBO.getRoleId(), sysUserRoleBO.getUserId());
        return Result.SUCCESS(remove);
    }

    @PostMapping("addBindUser")
    public Result addBindUser(@RequestBody SysUserRoleBO sysUserRoleBO) {
        List<SysUserRole> collect = sysUserRoleBO.getUserIdList().stream().map(x -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(Long.parseLong(x));
            sysUserRole.setRoleId(sysUserRoleBO.getRoleId());
            return sysUserRole;
        }).collect(Collectors.toList());
        sysUserRoleService.saveBatch(collect);
        return Result.SUCCESS();
    }

    @PostMapping("batchdeleteBindUser")
    public Result batchdeleteBindUser(@RequestBody SysUserRoleBO sysUserRoleBO) {
        sysUserRoleService.removeBatch(sysUserRoleBO.getRoleId(), sysUserRoleBO.getUserIdList());
        return Result.SUCCESS();
    }

    @GetMapping("checkUsername")
    public Result checkUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        return Result.SUCCESS(!ObjectUtils.isEmpty(sysUserService.getOne(wrapper)));
    }

}
